var pipeline = require('./pipeline'),
    sys = require('sys');

/**
 *
 */
function append_callback(value) {
  return function(s, callback) {
    if(s) {
      if(callback && typeof(callback) === 'function') {
        if(value) {
          callback(s+","+value);
       } else {
          callback(s);
        }
      }
    } else {
      if(callback && typeof(callback) === 'function') {
        callback(value);
      }
    }
  };
};

/**
 *
 */
function append_return(value) {
  return function(s) {
    return s+","+value;
  };
};

/**
 */
function append_x(value) {
  return value+',x';
};
/*
var test_create_inline = function(max) {
  var starttime = new Date().getTime();
  var back = 0;

  for(var i=0;i<max;i++){
    (function(_i) {
      //Create the pipeline processor inline to test the impact of creating it a shit load of times
      pipeline(append_callback(_i+':f1'), append_callback(_i+':f2'), append_callback(_i+':f3'), append_callback(_i+':f4'), append_callback(_i+':f5'))('start:'+_i, function(value) {
        //sys.puts("pipeline_test, i:"+_i+", value:"+value);
        back += 1;
        if(back === max) {
          var elapsed = (new Date().getTime() - starttime);
          sys.puts("test_create_inline: "+max+" calls took "+ elapsed +" milliseconds.  Thats "+(max/(elapsed/1000))+" pipelines per second bitches!");
        }
      });
    }(i));
  }
}
*/

/**
var test_create_before = function(max) {
  var starttime = new Date().getTime();
  var back = 0;

  //Create the pipelien processor before we enter the loop to reuse it
  var pipeline_test = pipeline(append_callback('f1'), append_callback('f2'), append_callback('f3'), append_callback('f4'), append_callback('f5'));

  for(var i=0;i<max;i++){
    (function(_i) {
      pipeline_test('start:'+_i, function(value) {
        //sys.puts("pipeline_test, i:"+_i+", value:"+value);
        back += 1;
        if(back === max) {
          var elapsed = (new Date().getTime() - starttime);
          sys.puts("test_create_before: "+max+" calls took "+ elapsed +" milliseconds.  Thats "+(max/(elapsed/1000))+" pipelines per second bitches!");
        }
      });
    }(i));
  }
}
*/

//test_create_inline(1000000);
//test_create_before(10);
function log(value) {
  sys.puts("log:"+value);
  return value;
};

var components = [append_return('f1'), 
                  append_callback('f2'), 
                  append_return('f3'), 
                  append_callback('f4'), 
                  append_return('f5'), 
                  append_callback('f6'), 
                  append_x,
                  log ];

var test_pipeline = new pipeline.Pipeline( components );
test_pipeline.addListener('complete', function() {
  sys.puts("pipeline complete");  
});

var obj = {
  test: 'test'
};

for(var i=0;i<1;i++) {
  test_pipeline.start('start:'+i); 
};

